#include "stdafx.hpp"
#include "ColligereUtils.hpp"
#include "ColligereCatraxxXmlDataImporter.hpp"

void col::CatraxxXmlDataImporter::Load ( const wxURI &rIn )
{
	wxASSERT ( rIn.GetScheme() == "file" );
	const wxString in_file ( wxURI ( rIn.GetPath() ).BuildUnescapedURI() );

	if ( !mInDoc.Load ( in_file ) )
	{
		throw DataImporterError ( rIn, _("Unable to load the input file.") );
	}
	if ( !IsValidCatraxxXmlFile() )
	{
		throw DataImporterError ( rIn, _("The input file is not a valid CATraxx XML-document." ) );
	}
}

wxUint32 col::CatraxxXmlDataImporter::GetItemCount() const
{
	return XmlUtils::GetItemCount ( mInDoc, "record" );
}

bool col::CatraxxXmlDataImporter::IsValidCatraxxXmlFile() const
{
	return mInDoc.IsOk() && mInDoc.GetRoot()->GetName() == "data";
}

void col::CatraxxXmlDataImporter::Import ( DatabaseInterfaceBase &rDest )
{
	DataImporterEvent evt_import_start ( COLLIGERE_EVT_IMPORT_START, rDest.GetId(), this );
	rDest.GetLayout()->GetEventHandler()->ProcessEvent ( evt_import_start );

	foreach ( const wxXmlNode *an, FindNodes ( mInDoc.GetRoot(), "record" ) )
	{
		Artist artist;
		InitializeItemFromNode ( an, artist );

		Album album;
		InitializeItemFromNode ( an, album );

		ParseTracks ( an, album, artist );

		artist.AddChild ( album );

		rDest.GetHandler()->AddItem ( artist );

		DataImporterEvent evt_item_imported 
			( COLLIGERE_EVT_ITEM_IMPORTED, rDest.GetId(), this, &album );
		rDest.GetLayout()->GetEventHandler()->ProcessEvent ( evt_item_imported );
	}

	DataImporterEvent evt_import_end ( COLLIGERE_EVT_IMPORT_END, rDest.GetId(), this );
	rDest.GetLayout()->GetEventHandler()->ProcessEvent ( evt_import_end );
}

void col::CatraxxXmlDataImporter::InitializeItemFromNode
( const wxXmlNode *pNode, DatabaseItemBase &rItem ) const
{
	using namespace boost;

	if ( Artist *artist = dynamic_cast < Artist * > ( &rItem ) )
	{
		artist->SetArtist ( GetNodeContent ( pNode, "name" ) );
		artist->SetCountry ( GetNodeContent ( pNode, "country" ) );
	}
	else if ( Album *album = 
		dynamic_cast < Album * > ( &rItem ) )
	{
		const wxString rcond ( GetNodeContent ( pNode, "conditionrecord" ) );
		const wxString ccond ( GetNodeContent ( pNode, "conditioncover" ) );

		wxString condition;

		if ( !rcond.empty() && !ccond.empty() )
		{
			condition = str ( format ( "%1% / %2%" ) % rcond % ccond );
		}
		else
		{
			condition = rcond.empty() ? ccond : rcond;
		}
		album->SetAlbum ( GetNodeContent ( pNode, "title") ).
			SetGenre ( GetNodeContent ( pNode, "fullgenre") ).
			SetFormat ( GetNodeContent ( pNode, "format") ).
			SetStatus ( GetNodeContent ( pNode, "status") ).
			SetExtInfo ( GetNodeContent ( pNode, "notes") ).
			SetLabel ( GetNodeContent ( pNode, "label") ).
			SetCatalogNum ( GetNodeContent ( pNode, "catalognumber") ).
			SetRating ( GetNodeContent ( pNode, "personalrating") ).
			SetSellPrice ( GetNodeContent ( pNode, "sellingprice") ).
			SetPurchasePrice ( GetNodeContent ( pNode, "price") ).
			SetCondition ( condition ).SetYear 
			( GetNodeAttribute ( FindNode ( pNode, "released" ), "year" ) );		
	}
	else if ( Track *track = 
		dynamic_cast < Track * > ( &rItem ) )
	{

	}
}

void col::CatraxxXmlDataImporter::ParseTracks 
( const wxXmlNode *pFromAlbumNode, Album &rToAlbum, 
 const Artist &rParentArtist ) const
{
	using namespace boost;

	foreach ( const wxXmlNode *node, FindNodes ( pFromAlbumNode, "track") )
	{
		wxASSERT ( node );

		Track track;

		const wxString track_artist ( GetNodeContent ( FindNode ( node, "name") ) );

		if ( track_artist != rParentArtist.GetArtist() )
		{
			track.SetTitle ( str ( format ( "%1% - %2%" ) % 
				track_artist % GetNodeContent ( FindNode ( node, "title") ) ) );
		}
		else
		{
			track.SetTitle ( GetNodeContent ( FindNode ( node, "title") ) );
		}

		track.SetNum ( wxString () << rToAlbum.GetChildCount() + 1 );
		track.SetLength ( GetNodeContent ( FindNode ( node, "length") ) );
		rToAlbum.AddChild ( track );
	}
}